/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import model.*;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author shinji
 */
@Named(value = "u_albumManagedBean")
@SessionScoped
public class AlbumManagedBean implements Serializable {

    private UploadedFile file;
    private List<UploadedFile> lstImages = new ArrayList<UploadedFile>();
    private List<Images> lstImage = new ArrayList<Images>();
    private static final int BUFFER_SIZE = 6124;
    private String path_to_upload = "/resources/user/images/album/";
    private String project_name = "Matrimony-war";
    private String hinhanh;

    public List<Images> getLstImage() {
        return lstImage;
    }

    public void setLstImage(List<Images> lstImage) {
        this.lstImage = lstImage;
    }

    public String getHinhanh() {
        return hinhanh;
    }

    public void setHinhanh(String hinhanh) {
        this.hinhanh = hinhanh;
    }
    
    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public void setImages(FileUploadEvent event){
        lstImages.add(event.getFile());
    }
    
    public void uploadImages(){
        for(UploadedFile uf : lstImages){
            upload(uf);
        }
        
        lstImages.clear();
    }
    
    private String upload(UploadedFile file) {
        String filename = "";
        if (file != null) {
            ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
            String path = extContext.getRealPath(this.path_to_upload + file.getFileName());
            filename = Calendar.getInstance().getTimeInMillis() + file.getFileName();
            if (path.indexOf("dist") != -1) {
                path = path.subSequence(0, path.indexOf("dist")).toString() + this.project_name + "\\web" + this.path_to_upload.replace("/", "\\");
                path = path.replace("\\", "/") + filename;
            }
            File result = new File(path);
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(result);
                byte[] buffer = new byte[BUFFER_SIZE];
                int bulk;
                InputStream inputStream = file.getInputstream();
                while (true) {
                    bulk = inputStream.read(buffer);
                    if (bulk < 0) {
                        break;
                    }
                    fileOutputStream.write(buffer, 0, bulk);
                    fileOutputStream.flush();
                }
                fileOutputStream.close();
                inputStream.close();
            } catch (IOException e) {
                filename = "";
            }
        }
        this.hinhanh = filename;
        return filename;
    }
    
    public String upload() {
        String filename = "";
        path_to_upload = "/resources/user/images/avatar/";
        
        if (file != null) {
            ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
            String path = extContext.getRealPath(this.path_to_upload + file.getFileName());
            filename = Calendar.getInstance().getTimeInMillis() + file.getFileName();
            if (path.indexOf("dist") != -1) {
                path = path.subSequence(0, path.indexOf("dist")).toString() + this.project_name + "\\web" + this.path_to_upload.replace("/", "\\");
                path = path.replace("\\", "/") + filename;
            }
            File result = new File(path);
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(result);
                byte[] buffer = new byte[BUFFER_SIZE];
                int bulk;
                InputStream inputStream = file.getInputstream();
                while (true) {
                    bulk = inputStream.read(buffer);
                    if (bulk < 0) {
                        break;
                    }
                    fileOutputStream.write(buffer, 0, bulk);
                    fileOutputStream.flush();
                }
                fileOutputStream.close();
                inputStream.close();
            } catch (IOException e) {
                filename = "";
            }
        }
        this.hinhanh = filename;
        return filename;
    }
}
